package lol.clann.bukkit.pluginboot.util;

import lol.clann.clannboot.annotation.svc.PostInstance;
import lol.clann.clannboot.annotation.svc.Service;
import lol.clann.clannboot.util.clazz.ClassUtils;
import lol.clann.clannboot.util.clazz.MethodFilter;
import lol.clann.clannboot.util.clazz.WrapClass;
import lol.clann.clannboot.util.clazz.WrapMethod;

/**
 * @author pyz
 * @date 2018/10/7 下午4:26
 */
@Service(priority = Integer.MAX_VALUE)
public class ChatSerializerUtils {

    public static final WrapClass classChatSerializer = ClassUtils.forName("{nms}.ChatSerializer", "{nms}.IChatBaseComponent$ChatSerializer");
    public static final WrapClass classIChatBaseComponent = ClassUtils.forName("{nms}.IChatBaseComponent");

    private WrapMethod<String> serialize;
    private WrapMethod deserialize;

    @PostInstance
    private void init() {
        serialize = classChatSerializer.getMethod(MethodFilter.build().returnType(String.class).parameterType(classIChatBaseComponent).static_(true));
        deserialize = classChatSerializer.getMethod(MethodFilter.build().parameterType(String.class).returnType(classIChatBaseComponent).static_(true).index(0));
    }

    public String serialize(Object iChatBaseComponent) {
        return serialize.invoke(null, iChatBaseComponent);
    }

    public Object deserialize(String content) {
        return deserialize.invoke(null, content);
    }
}
